package com.zhangxu.microservice.employee.domain.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.utils.AssertUtils;
import com.zhangxu.microservice.employee.common.enums.EmployeeStatusEnum;
import com.zhangxu.microservice.employee.dao.condition.EmployeeCondition;
import com.zhangxu.microservice.employee.dao.entity.EmployeeDO;
import com.zhangxu.microservice.employee.domain.dto.CreateEmployeeDTO;
import com.zhangxu.microservice.employee.domain.mapstrcut.EmployeeDomainConverter;
import com.zhangxu.microservice.employee.dao.service.EmployeeDao;
import com.zhangxu.microservice.employee.domain.dto.EmployeeDTO;
import com.zhangxu.microservice.employee.domain.service.EmployeeDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class EmployeeDomainServiceImpl implements EmployeeDomainService {

    private final EmployeeDao employeeDao;
    private final EmployeeDomainConverter employeeDomainConverter;

    @Override
    public Long createEmployee(CreateEmployeeDTO createUserDTO) {
        EmployeeDO employeeDO = employeeDomainConverter.toEmployeeDO(createUserDTO);
        employeeDao.save(employeeDO);
        return employeeDO.getId();
    }

    @Override
    public EmployeeDTO getByAccount(String account) {
        EmployeeDO userDO = employeeDao.getByAccount(account);
        return employeeDomainConverter.toEmployeeDTO(userDO);
    }

    @Override
    public EmployeeDTO getById(Long id) {
        return employeeDomainConverter.toEmployeeDTO(employeeDao.getById(id));
    }

    @Override
    public IPage<EmployeeDTO> page(EmployeeCondition condition) {
        IPage<EmployeeDO> page = employeeDao.page(condition);
        return page.convert(employeeDomainConverter::toEmployeeDTO);
    }

    @Override
    public void enable(Long employeeId) {
        EmployeeDTO employeeDTO = getById(employeeId);
        AssertUtils.notNull(employeeDTO, "数据不存在");
        //判断只能新建状态可以启用
        AssertUtils.isFalse(EmployeeStatusEnum.ENABLE.name().equals(employeeDTO.getStatus()), "当前状态不能启用");
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(employeeId);
        employeeDO.setStatus(EmployeeStatusEnum.ENABLE.name());
        employeeDao.updateById(employeeDO);
    }

    @Override
    public void disable(Long employeeId) {
        EmployeeDTO employeeDTO = getById(employeeId);
        AssertUtils.notNull(employeeDTO, "数据不存在");
        //判断只能新建状态可以启用
        AssertUtils.isTrue(EmployeeStatusEnum.ENABLE.name().equals(employeeDTO.getStatus()), "当前状态不能禁用");
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(employeeId);
        employeeDO.setStatus(EmployeeStatusEnum.DISABLE.name());
        employeeDao.updateById(employeeDO);
    }

    @Override
    public void delete(Long employeeId) {
        EmployeeDTO employeeDTO = getById(employeeId);
        AssertUtils.notNull(employeeDTO, "数据不存在");
        //判断只能新建状态可以启用
        AssertUtils.isTrue(EmployeeStatusEnum.INIT.name().equals(employeeDTO.getStatus()), "当前状态不能删除");
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(employeeId);
        employeeDO.setDeleteTime(System.currentTimeMillis());
        employeeDao.updateById(employeeDO);
    }

    @Override
    public List<EmployeeDTO> listByIds(List<Long> employeeIds) {
        return employeeDao.listByIds(employeeIds).stream().map(employeeDomainConverter::toEmployeeDTO).toList();
    }
}
